package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.SysBusinessModuleMenu;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.mapper.SysBusinessModuleMenuMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysBusinessModuleBo;
import com.ruoyi.system.domain.vo.SysBusinessModuleVo;
import com.ruoyi.system.domain.SysBusinessModule;
import com.ruoyi.system.mapper.SysBusinessModuleMapper;
import com.ruoyi.system.service.ISysBusinessModuleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 租户模块Service业务层处理
 *
 * @author pengju.liao
 * @date 2022-03-21
 */
@RequiredArgsConstructor
@Service
public class SysBusinessModuleServiceImpl implements ISysBusinessModuleService {

    private final SysBusinessModuleMapper baseMapper;

    private final SysBusinessModuleMenuMapper baseMenuMapper;

    /**
     * 查询租户模块
     *
     * @param id 租户模块主键
     * @return 租户模块
     */
    @Override
    public SysBusinessModuleVo queryById(Long id) {
        SysBusinessModuleVo vo = baseMapper.selectVoById(id);
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询租户模块
     *
     * @param moduleId 租户模块主键
     * @return 租户模块
     */
    @Override
    public List<Long> selectMenuListByModuleId(Long moduleId) {
        return baseMenuMapper.selectMenuListByModuleId(moduleId);
    }

    /**
     * 查询租户模块列表
     *
     * @param bo 租户模块
     * @return 租户模块
     */
    @Override
    public TableDataInfo<SysBusinessModuleVo> queryPageList(SysBusinessModuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBusinessModule> lqw = buildQueryWrapper(bo);
        Page<SysBusinessModuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询租户模块列表
     *
     * @param bo 租户模块
     * @return 租户模块
     */
    @Override
    public List<SysBusinessModuleVo> queryList(SysBusinessModuleBo bo) {
        LambdaQueryWrapper<SysBusinessModule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBusinessModule> buildQueryWrapper(SysBusinessModuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBusinessModule> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getModuleName()), SysBusinessModule::getModuleName, bo.getModuleName());
        return lqw;
    }

    /**
     * 新增租户模块
     *
     * @param bo 租户模块
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SysBusinessModuleBo bo) {
        SysBusinessModule add = BeanUtil.toBean(bo, SysBusinessModule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        // TODO 添加菜单权限
        insertRoleMenu(bo);
        return flag;
    }

    /**
     * 新增角色菜单信息
     *
     * @param bo 租户模块对象
     */
    public int insertRoleMenu(SysBusinessModuleBo bo) {
        int rows = 1;
        // 新增用户与角色管理
        List<SysBusinessModuleMenu> list = new ArrayList<>();
        for (Long menuId : bo.getMenuIds()) {
            SysBusinessModuleMenu bmm = new SysBusinessModuleMenu();
            bmm.setModuleId(bo.getId());
            bmm.setMenuId(menuId);
            list.add(bmm);
        }
        if (list.size() > 0) {
            rows = baseMenuMapper.insertBatch(list) ? list.size() : 0;
        }
        return rows;
    }

    /**
     * 修改租户模块
     *
     * @param bo 租户模块
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SysBusinessModuleBo bo) {
        SysBusinessModule update = BeanUtil.toBean(bo, SysBusinessModule.class);
        validEntityBeforeSave(update);
        // 先删除 后保存
        Map<String, Object> map = new HashMap<>();
        map.put("module_id", bo.getId());
        baseMenuMapper.deleteByMap(map);
        insertRoleMenu(bo);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysBusinessModule entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除租户模块
     *
     * @param ids 需要删除的租户模块主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            Map<String, Object> map = new HashMap<>();
            map.put("module_id", id);
            baseMenuMapper.deleteByMap(map);
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
